package com.sunny.ihealth;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.data.PreferencesHelper;
import com.sunny.ihealth.data.im.IMMessageHandler;
import com.sunny.ihealth.entity.eventbus.UserChangeEvent;
import com.sunny.ihealth.service.CoreService;

import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePalApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import cn.bmob.newim.BmobIM;

/**
 * 自定义Application，初始化数据
 * Created by Sunny  An on 2016/5/16.
 */
public class BaseApplication extends Application {

    private static final String TAG = "TAG::BaseApplication";

    private BmobManager bmob = null;
    private PreferencesHelper preferencesHelper = null;
    //当前的用户账号
    private String currentUser = null;

    //是否要更新首页状态
    private boolean updateState = false;

    private IMMessageHandler imMessageHandler = null;

    @Override
    public void onCreate() {
        super.onCreate();

        if (getApplicationInfo().packageName.equals(getMyProcessName())) {
            bmob = getBmob();
            preferencesHelper = getPreferencesHelper();
            LitePalApplication.initialize(this);
            startService(new Intent(getApplicationContext(), CoreService.class));
            //NewIM初始化
            BmobIM.init(this);
            //注册消息接收器
            imMessageHandler = new IMMessageHandler();
            BmobIM.registerDefaultMessageHandler(imMessageHandler);
            //启动百度地图
            SDKInitializer.initialize(getApplicationContext());
        }


    }

    public IMMessageHandler getImMessageHandler() {
        if (imMessageHandler == null) {
            //注册消息接收器
            imMessageHandler = new IMMessageHandler();
            BmobIM.registerDefaultMessageHandler(imMessageHandler);
        }
        return imMessageHandler;
    }

    public BmobManager getBmob() {
        if (bmob == null) {
            Log.e(TAG, "bmob is NULL");
            bmob = new BmobManager(this);
        }
        return bmob;
    }

    public PreferencesHelper getPreferencesHelper() {
        if (preferencesHelper == null) {
            Log.e(TAG, "preferencesHelper is NULL");
            preferencesHelper = new PreferencesHelper(this);
        }
        return preferencesHelper;
    }


    public boolean getUpdateState() {
        return updateState;
    }

    public void setUpdateState(boolean updateState) {
        this.updateState = updateState;
    }

    public String getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(String currentUser) {
        //发送消息，用户已改变
        UserChangeEvent event = new UserChangeEvent();
        event.setOldUsername(this.currentUser);
        this.currentUser = currentUser;
        EventBus.getDefault().post(event);

    }


    /**
     * 获取当前运行的进程名
     *
     * @return
     */
    public static String getMyProcessName() {
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
